﻿using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Phone.Shell;
using Newtonsoft.Json.Linq;

namespace BackgroundSigner
{
    public class SignService
    {
        public static async Task<SignResult> SignAsync()
        {
            if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                return new SignResult() { FailReason = "无网络连接" };
            }

            //TODO: Add code to perform your task in background
            var request = (HttpWebRequest)WebRequest.Create("http://point.wps.cn/kpoints/submit/sign/");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";


            var stream = await request.GetRequestStreamAsync();

            var xml =
                "<?xml version='1.0' encoding='utf-8' ?><xLive><token>41d6d1f418034143912689881f8e8303-667273959</token><userId>2581812</userId></xLive>";
            var bytes = System.Text.Encoding.UTF8.GetBytes(xml);
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
            var response = await request.GetResponseAsync();

            if (((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
            {
                return new SignResult() { FailReason = "网络原因" };
            }

            var rs = response.GetResponseStream();
            var reader = new StreamReader(rs);
            var result = reader.ReadToEnd();
            response.Close();
            rs.Close();

            var o = JObject.Parse(result);

            try
            {
                var state = o["state"].Value<int>();

                if (state == 1)
                {
                    return new SignResult() { RewardAmount = o["rewardsize"].Value<int>(), Succeed = true };
                }
                if (state == -102)
                {
                    return new SignResult() { Succeed = false, FailReason = "今天已经签过到了" };
                }

                return new SignResult() { FailReason = "服务器应答格式错误, state值未定义" };

            }
            catch (Exception ex)
            {
                return new SignResult() { FailReason = "服务器应答格式错误 " + ex.Message };
            }
        }

    }

    public class SignResult
    {
        public bool Succeed { get; set; }
        public int RewardAmount { get; set; }
        public string FailReason { get; set; }

        public override string ToString()
        {
            if (Succeed)
            {
                return "签到成功," + "获得奖励" + RewardAmount + "MB";
            }

            return "签到失败," + FailReason;
        }
    }
}
